package com.company.task1;

/*
使用 List 集合实现简易的学生信息管理系统，要求打印字符界面提示用户选择相应的功 能，根据用户输入的选择去实现增加、删除、修改、查找以及遍历所有学生信息的功能。

 其中学生的信息有：学号、姓名、年龄。 要求： 尽量将功能拆分为多个.java 文件。

 */

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.List;

public class StudentInfoTest {

    public static void main(String[] args) {

        // 读取文件至ArrayList中
        String data = null;
        String[] strList;
        List<ArrayList> studentList = new ArrayList<ArrayList>();

        InputStreamReader read = null;
        BufferedReader reader = null;

        try{

            read = new InputStreamReader(new FileInputStream(System.getProperty("user.dir") + "/src/com/company/task1/StudentInfo.txt"));
            reader = new BufferedReader(read);

            while((data = reader.readLine()) != null){

                strList = data.split(",");
                ArrayList arrList = new ArrayList();
                Collections.addAll(arrList, strList);
                studentList.add(arrList);

            }
        } catch (IOException e){

            e.printStackTrace();

        } finally {

            if(null != read){
                try {
                    read.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(null != reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        int functionNo;
        boolean isExit = false;

        while(!isExit){

            System.out.println("请选择一个功能：1、增加  2、删除  3、修改  4、查找  5、遍历  6、退出系统");
            Scanner sc = new Scanner(System.in);
            functionNo = sc.nextInt();

            switch (functionNo){
                case 1:
                    try {
                        AddStudentInfo.addFunction(studentList);
                    } catch (AgeException e) {
                        e.printStackTrace();
                        return;
                    } catch (StudentNoException e) {
                        e.printStackTrace();
                        return;
                    }
                    break;
                case 2:
                    DeleteStudentInfo.deleteFunction(studentList);
                    break;
                case 3:
                    try {
                        EditStudentInfo.editFunction(studentList);
                    } catch (StudentNoException e) {
                        e.printStackTrace();
                        return;
                    } catch (AgeException e) {
                        e.printStackTrace();
                        return;
                    }
                    break;
                case 4:
                    SearchStudentInfo.searchFunction(studentList);
                    break;
                case 5:
                    TraverseStudentInfo.traverseFunction(studentList);
                    break;
                case 6:
                    isExit = true;
                    break;
                default:
                    System.out.println("选项编号不存在，请重新选择！");
                    break;
            }
            System.out.println("现在的学生列表为：" + studentList);
        }

        // 写入学生列表数据进文件中
        BufferedWriter bw = null;

        try{

            System.out.println("正在写入数据...");
            bw = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/src/com/company/task1/StudentInfo.txt"));

            for(ArrayList l: studentList){

                StringBuilder sb = new StringBuilder();

                for(Object o: l){

                    sb.append(o).append(",");

                }

                bw.write(sb.toString());
                bw.newLine();
                bw.flush();

            }
        } catch (IOException e){

            e.printStackTrace();

        } finally {

            if(null != bw){

                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("写入数据成功！");
        }

        System.out.println("已登出，欢迎下次使用");
    }
}
